/**
 * 定义站点分析仪管理模块
 */
(function(window, angular, undefined) {
	var module = angular.module('water.apps.nanshan.systemConfig.stationEquipment.stationEquipmentManager',['water.entity.dataCollectionInstrumet',
							'water.entity.monitorEquipment','water.entity.monitorEquipmentDictParam','water.entity.monitorStation' ]);

	module.controller('stationEquipmentManagerController', function($rootScope,
			$scope, $monitorEquipmentService, $monitorStationService,$monitorEquipmentDictParamService) {

		/**
		 * 编辑对话框对象
		 */
		$scope.editDialog;
		/**
		 * 详情对话框对象
		 */
		$scope.detailDialog;
		/**
		 * 新增对话框
		 */
		$scope.createDialog;
		/**
		 * 编辑对象
		 */
		$scope.editModel = {};
		/**
		 * 新增对象
		 */
		$scope.createModel = {};
		/**
		 * 详情对象
		 */
		$scope.detailModel = {};
		/**
		 * 初始化站点选择数据
		 */
		$scope.stationSelectList;
		/**
		 * 监测仪器数据
		 */
		$scope.equipments = [];
		/**
		 * 监测项数据
		 */
		$scope.monitorParams;
		/**
		 * 详情按钮点击事件
		 */
		$scope.onDetailClick = function(data) {
			$monitorEquipmentService.getModel(data.GUID).then(function(d) {
				$scope.detailModel = d;
				console.log("onDetailClick###", data.GUID, d);
				$scope.POLL_NAMES="";
				$scope.detailModel.BC_SITE_POLL_LIST && $scope.detailModel.BC_SITE_POLL_LIST.forEach(function(data){
					$scope.POLL_NAMES+=data.NAME_DESC+",";
				});
				$scope.POLL_NAMES && $scope.POLL_NAMES.substring(0,$scope.detailModel.POLL_NAMES.length-1);
				$scope.detailModel.MONITOR_POINT=$scope.detailModel.NAME.substring(0,$scope.detailModel.NAME.indexOf("-"));
				$scope.detailModel.NAME=$scope.detailModel.NAME.substring($scope.detailModel.NAME.indexOf("-")+1);
				$scope.detailDialog.open();
			});
		}
		/**
		 * 编辑按钮点击事件
		 */
		$scope.onEditClick = function(data) {
			$monitorEquipmentService.getModel(data.GUID).then(function(d) {
				console.log("onEditClick###", d);
				$scope.editModel = d;
				$scope.editDialog.open();
			});
		}
		/**
		 * 提交编辑按钮点击事件
		 */
		$scope.onSubmitEditClick = function(data) {
			data = $scope.updatePollListStationId(data);
			$monitorEquipmentService.updateModel(data).then(function() {
				$.messager.alert("保存成功", "保存成功", 'info');
				$scope._getEquipmentList();
				$scope.editDialog.close();
			});

		}
		/**
		 * 新增按钮点击事件
		 */
		$scope.monitorStations = [];
		$scope.onCreateClick = function() {
			console.log("onCreateClick###",$scope.monitorStations);
			$scope.createDialog.open();
		}
		/**
		 * 提交新建按钮点击事件
		 */
		$scope.onSubmitCreateClick = function(data) {
			data = $scope.updatePollListStationId(data);
			changeName();
			console.error("onSubmitCreateClick###", data);
			$monitorEquipmentService.createModel(data).then(function() {
				$.messager.alert("保存成功", "保存成功", 'info');
				$scope.createDialog.close();
				$scope._getEquipmentList();
			});
		}
		/**
		 * 删除仪器
		 */
		$scope.onDeleteClick = function(data) {
			$.messager.confirm("确认", "确定要删除这条记录吗？", function(r) {
				if (r) {
					$monitorEquipmentService.deleteModel(data.GUID).then(
							function() {
								$.messager.alert("删除成功", "删除成功", 'info');
								$scope._getEquipmentList();
							});
				}
			});

		}
		/**
		 * 如果设置了站点污染物，把所有站点污染物的站点id更新
		 */
		$scope.updatePollListStationId = function(data) {
			if (data.BC_SITE_POLL_LIST) {
				for (var i = 0; i < data.BC_SITE_POLL_LIST.length; i++) {
					data.BC_SITE_POLL_LIST[i].SITE_ID = data.MONITOR_POINT;
				}
			}
			return data;
		}
		/**
		 * 选中分析仪时更新相关监测项
		 */
		$scope.onEquipmentSelectChange = function(key) {
			$monitorEquipmentDictParamService.getList({
				EQUIPMENT_ID : key
			}).then(function(d) {
				console.log("onEquipmentSelectChange###", key, d.data);
				$scope.monitorParams = "";
				$scope.createModel.nextNAME = d.data[0].EQUIPMENT_ID;
				$scope.createModel.BC_SITE_POLL_LIST = [];
				for (i = 0; i < d.data.length; i++) {
					$scope.createModel.BC_SITE_POLL_LIST[i] = {
						NAME : d.data[i].MONITOR_POLLUTANTS
					}
					$scope.monitorParams += d.data[i].PARAM_CODE + " ";
				}

			});
		};
		/**
		 * 选中站点时更新相关监测项
		 */
		$scope.onStationSelectChange = function(station) {
			console.log("onStationSelectChange###", station);
			$scope.createModel.preNAME = station.v;
		}
		/**
		 * 自动设置站点分析仪名称
		 */
		function changeName() {
			var pre = $scope.createModel.preNAME || "";
			var next = $scope.createModel.nextNAME || "";
			$scope.createModel.NAME = pre + "-" + next;
		}
		/**
		 * 刷新列表
		 */
		function onRefreshClick() {
			$scope._getEquipmentList();
		}
		/**
		 * 获取仪器列表
		 */
		$scope._getEquipmentList = function() {
			$monitorEquipmentService.getList({
				CITY_CODE : '001002',
					DISTRICT:'00100202',
					WWTP_ID:'nanshanqu'
			}).then(function(d) {
				$scope.equipments = d;
			});
			$monitorStationService.getList({
				CITY_CODE : '001002',
				DISTRICT:'00100202',
				WWTP_ID:'nanshanqu'
			}).then(function(d) {
				console.log("_getEquipmentList###", $scope.equipments);
				for (i = 0; i < d.length; i++) {
					$scope.monitorStations[i] = {
						k : d[i].CODE,
						v : d[i].NAME
					};
				}
			});
		}
		/**
		 * 获取站点列表，编辑或者新增的时候需要选择
		 */
		$scope._getStationSelectList = function() {
			$monitorStationService.getDomainList().then(function(d) {
				$scope.stationSelectList = d;
			});
		};

		$scope._getEquipmentList();
		$scope._getStationSelectList();
	});
}(window, angular));